/*
	File: data/scripts/globals.nut
	Author: Astrofra
*/

//	Binary to Decimal conversion
//	Input	: string
//	Output	: integer
function	aBinToDec(str, debug_info = "")
{
	local	i, l, c, d = 0, exp = 1
	l = str.len()
	for (i = 0; i < l; i++)
	{
		c = str.slice(l - i - 1, l - i)
		if (c == "1")
			d += exp
		exp *= 2
	}

	if (debug_info != "")	print("aBinToDec() : " + debug_info + " = " + d.tostring()) 

	return d
}

//	Sharper Sin()
//	See : http://goo.gl/ykwsA
function	sin2(a)
{
	local	_val,
			_sign

	_val = sin(a)
	_sign = _val < 0.0?-1.0:1.0
	_val = Abs(_val)
	_val = pow(_val, 0.5) //_val *= _val
	_val = _sign * _val
	return _val
}

//	Sharper Cos()
function	cos2(a)
{
	local	_val,
			_sign

	_val = cos(a)
	_sign = _val < 0.0?-1.0:1.0
	_val = Abs(_val)
	_val = pow(_val, 0.5) //_val *= _val
	_val = _sign * _val
	return _val
}

colors	<-	{
			orange	= Vector(1.0,0.485,0.05),
			blue	= Vector(0.1,0.9,1.0),
			green	= Vector(0.1, 1.0, 0.2),
			red		= Vector(1.0, 0.05, 0.0)
		}


with_mask	<-	{	ground			= aBinToDec("11111", "with ground")
					enemy 			= aBinToDec("11011", "with enemy")
					bullet_enemy 	= aBinToDec("01001", "with bullet enemy")
					player 			= aBinToDec("00111", "with player")
					bullet_player 	= aBinToDec("00011", "with bullet player") 
			}

self_mask	<-	{	ground			= aBinToDec("00001", "self ground")
					enemy 			= aBinToDec("00010", "self enemy")
					bullet_enemy 	= aBinToDec("00100", "self bullet enemy")
					player 			= aBinToDec("01000", "self player")
					bullet_player 	= aBinToDec("10000", "self bullet player") 
			}

//	Loads a wav file from the specific folder of the game
//	Otherwise prints an error and returns 0
function	LoadGameSfx(fname)
{
	local	filename
	filename = "data/sfx/" + fname

	if (FileExists(filename))
	{
		print("LoadGameSfx() : Found : '" + filename + "'.")		
		return (EngineLoadSound(g_engine, filename))
	}
	else
		print("LoadGameSfx() : Cannot find file : '" + filename + "'.")

	return	0 
}

//	Creates a unique id string based on
//	the name of the item and it's original
//	position in space.
//---------------------------------
function	CreateItemHashKey(screen_name, item, key_name)
//---------------------------------
{
	local	_item_hash_key	
	_item_hash_key = screen_name + "_" + ItemGetName(item) + "_"
	_item_hash_key += (Abs(ItemGetWorldPosition(item).x * 100.0 + 1000.0)).tointeger().tostring() + "_"
	//_item_hash_key += (Abs(ItemGetWorldPosition(item).y * 100.0 + 1000.0)).tointeger().tostring() + "_"
	_item_hash_key += (Abs(ItemGetWorldPosition(item).z * 100.0 + 1000.0)).tointeger().tostring()
	_item_hash_key += "_" + key_name

	return (_item_hash_key)
}

function	AimError(range_error)
{
	local	err = Vector(Rand(-1.0, 1.0), 0, Rand(-1.0, 1.0)).Normalize().Scale(range_error)
	return (err)
}
				
//-----------------------------
function	MakeTriangleWave(i)
//-----------------------------
// 1 ^   ^
//   |  / \
//   | /   \
//   |/     \
//   +-------->
// 0    0.5    1
{
	local 	s = Sign(i);
	i = Abs(i);

	if (i < 0.5)
		return (s * i * 2.0);
	else
		return (s * (1.0 - (2.0 * (i - 0.5))));
}